// 메인 액티비티
package com.example.mytimeclock;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class TimerMain extends ListActivity{
	ArrayList<MyItem> listTimer;
	MyItem myitem;
	MyListAdapter mAdapter;
	
	Button mBtnStart;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.overridePendingTransition(R.anim.start_enter, R.anim.start_exit);// 액티비티 전환 애니메이션
		setContentView(R.layout.clock_main);
		
		listTimer = new ArrayList<MyItem>();
		
		mAdapter = new MyListAdapter(this, R.layout.timerlist, listTimer);
		
		mBtnStart = (Button) findViewById(R.id.start_btn);
		setListAdapter(mAdapter);
		
	}
	
	protected void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id) {
		Toast.makeText(this, listTimer.get(position).name, Toast.LENGTH_SHORT).show();
		
	}
	
	public void mOnClick(View v){ // 메인 액티비티 화면의 버튼클릭 메소드
		final LinearLayout linear;
		switch (v.getId()){
		case R.id.start_btn:// 새일정시작 버튼 클릭시 대화상자 띄우기
			linear = (LinearLayout) View.inflate(this, R.layout.box, null);
			new AlertDialog.Builder(this)
			.setTitle("일정 이름을 입력하세요")
			.setView(linear)
			.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					EditText name = (EditText) linear.findViewById(R.id.box_name_edit);
					String txtname = name.getText().toString();
					if(txtname.length() != 0){
						myitem =new MyItem(txtname, MyItem.chrono, nowTime());
						listTimer.add(myitem);
					}
				}
			})
			.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					
				}
			})
			.show();
			break;
		} // 스위치문 끝
	} // mOnClick 메소드 끝
	
	
	public static String nowTime() {	// 현재시각 메소드
		String time = new String();
		Calendar cal = new GregorianCalendar();
		
		time= String.format("%d월 %d일 %02d:%02d ",
				cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH),
				cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
		return time;
	}// 현재시각 메소드 끝
	
	public void finish() {
		super.finish();
		this.overridePendingTransition(R.anim.end_enter, R.anim.end_exit);// 액티비티 전환 애니메이션
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}